//
//  DreamOfGalaxyViewController.m
//  ProjectB
//
//  Created by lanou on 16/5/6.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "DreamOfGalaxyViewController.h"
#import "GalaxyTableViewController.h"
#import "MyWishViewController.h"
#import "TheMessageBoxTableViewController.h"
#import "UIView+Extension.h"

@interface DreamOfGalaxyViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic,strong) UIView *btnIndicator;

@end

@implementation DreamOfGalaxyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.items.firstObject.backBarButtonItem = [UIBarButtonItem createBackItem];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
        
    self.navigationItem.title = @"心愿的银河";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupChildViewControllers];
    [self.view addSubview:self.titleView];
    [self.view addSubview:self.scrollView];
    [self setupTitleBtn];
    [self.titleView addSubview:self.btnIndicator];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"掷入" style:UIBarButtonItemStylePlain target:self action:@selector(throw)];
    self.navigationItem.rightBarButtonItem = item;
    
    // Do any additional setup after loading the view.
}





//设置子控制器
- (void)setupChildViewControllers{
    GalaxyTableViewController *galaxyVC = [[GalaxyTableViewController alloc] init];
    [self addChildViewController:galaxyVC];
    
    MyWishViewController *myWishVC = [[MyWishViewController alloc] init];
    [self addChildViewController:myWishVC];

    TheMessageBoxTableViewController *messageVC = [[TheMessageBoxTableViewController alloc] init];
    [self addChildViewController:messageVC];
    
}

//设置titleView按钮
- (void)setupTitleBtn{
    NSArray *titles = @[@"全部心愿",@"我的心愿",@"消息匣子"];
    NSInteger count = titles.count;
    
    CGFloat btnH = self.titleView.height - 2;
    CGFloat btnW = self.titleView.width / count;
    CGFloat btnY = 0;
    for (NSInteger i = 0; i < count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        CGFloat btnX = btnW * i;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [self.titleView addSubview:btn];
        //默认选中第一个按钮
        if (btn.tag == 0) {
            btn.enabled = NO;
            self.selectedBtn = btn;
            [btn.titleLabel sizeToFit];
  
            self.btnIndicator.width = btn.titleLabel.width;
            self.btnIndicator.centerX = btn.centerX;
        }
    }
}

//按钮点击事件
-(void)btnClick:(UIButton *)btn {
    //按钮状态
    self.selectedBtn.enabled = YES;
    btn.enabled = NO;
    self.selectedBtn = btn;
    //指示器动画
    [UIView animateWithDuration:0.2 animations:^{
        //必须在这里才设置指示器的宽度，要不宽度会计算错误
        self.btnIndicator.width = btn.titleLabel.width;
        self.btnIndicator.centerX = btn.centerX;
    }];
    //控制器view的滚动
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = btn.tag * self.scrollView.width;
    offset.x = btn.tag * self.scrollView.self.frame.size.width;
    [self.scrollView setContentOffset:offset animated:YES];
    
}

//右导航栏点击按钮事件
- (void)throw{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"你没有可掷的心愿" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
}


#pragma  mark - UIScrollViewDelegate

//结束滚动时动画
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    //计算当前控制器View索引
    NSInteger index = scrollView.contentOffset.x / scrollView.self.frame.size.width;
    
    //取出子控制器
    UITableViewController *viewVc = self.childViewControllers[index];
    viewVc.view.x = scrollView.contentOffset.x;
    viewVc.view.y = 0;
    viewVc.view.height = scrollView.height;

    
    [scrollView addSubview:viewVc.view];
    
}

//滚动减速时
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    //点击titleView按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self btnClick:self.titleView.subviews[index]];
    
}



#pragma mark - getter and setter

- (UIView *)titleView{
    if (_titleView == nil) {
        _titleView = [[UIView alloc]init];
        _titleView.frame = CGRectMake(0, 64, self.view.width, 35);
        _titleView.tintColor = [UIColor blackColor];
        _titleView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.7];
    }
    return _titleView;
}


- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.frame = CGRectMake(0, 64 + 35, self.view.frame.size.width, self.view.frame.size.height - 64 - 35);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;//分页
        _scrollView.contentSize = CGSizeMake(_scrollView.width * self.childViewControllers.count, 0);
        // 添加第一个控制器的view
        [self scrollViewDidEndScrollingAnimation:self.scrollView];
      
    }
    return _scrollView;
}

- (UIView *)btnIndicator{
    if (_btnIndicator == nil) {
        _btnIndicator = [[UIView alloc]init];
        _btnIndicator.backgroundColor = [UIColor redColor];
        
        _btnIndicator.height = 2;
        _btnIndicator.y = self.titleView.height - _btnIndicator.height;
    }
    return _btnIndicator;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
